package csc.com.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import csc.com.repository.Users;
import csc.com.service.UserMapper;

public class UserDAO {

	private JdbcTemplate jdbcTemplateObject;
	private UserMapper userMapper;
	
	public UserMapper getUserMapper() {
		return userMapper;
	}
	@Autowired
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}
	public JdbcTemplate getJdbcTemplateObject() {
		return jdbcTemplateObject;
	}
	@Autowired
	public void setJdbcTemplateObject(JdbcTemplate jdbcTemplateObject) {
		this.jdbcTemplateObject = jdbcTemplateObject;
	}

	public Users getUser(String loginId, String password) {
		String SQL = "select * from users where LoginId = ? and Password = ?";
		try {
			Users user = jdbcTemplateObject.queryForObject(SQL, new Object[] {
					loginId, password }, userMapper);
			return user;
		} catch (EmptyResultDataAccessException ex) {

		}
		return new Users();
	}
	public void addUser(Users user) {
		// TODO Auto-generated method stub
		String SQL = "insert into users (LoginId, FirstName, LastName, Password, IdCardNumber, Email) values (?, ?, ?, ?, ?, ?)";

		jdbcTemplateObject.update(SQL, user.getLoginId(), user.getFirstName(), user.getLastName(), 
				user.getPassword(), user.getIdCardNumber(), user.getEmail());
		return;
	}
	public List<Users> getUsers() {
		// TODO Auto-generated method stub
		String SQL = "select * from users";
		List<Users> users = jdbcTemplateObject.query(SQL, userMapper);
		return users;
	}
}
